import request from '@/request'

export interface AnnouncementAddRequest {
  title: string
  content: string
  type?: number
  isTop?: number
  expireTime?: string
}

export interface AnnouncementEditRequest {
  id: string
  title?: string
  content?: string
  type?: number
  isTop?: number
  status?: number
  expireTime?: string
}

export interface AnnouncementQueryRequest {
  current?: number
  pageSize?: number
  id?: string
  title?: string
  type?: number
  isTop?: number
  status?: number
  userId?: string
}

export interface AnnouncementVO {
  id: string
  title: string
  content: string
  type: number
  isTop: number
  status: number
  publishTime: string
  expireTime?: string
  user: any
  isRead?: boolean
  createTime: string
  updateTime: string
}

/**
 * 创建公告（仅管理员）
 */
export function addAnnouncement(announcementAddRequest: AnnouncementAddRequest) {
  return request<number>({
    url: '/api/announcement/add',
    method: 'POST',
    data: announcementAddRequest
  })
}

/**
 * 删除公告（仅管理员）
 */
export function deleteAnnouncement(id: string) {
  return request<boolean>({
    url: '/api/announcement/delete',
    method: 'POST',
    data: { id }
  })
}

/**
 * 更新公告（仅管理员）
 */
export function updateAnnouncement(announcementEditRequest: AnnouncementEditRequest) {
  return request<boolean>({
    url: '/api/announcement/update',
    method: 'POST',
    data: announcementEditRequest
  })
}

/**
 * 根据 id 获取公告（仅管理员）
 */
export function getAnnouncementById(id: string) {
  return request<any>({
    url: '/api/announcement/get',
    method: 'GET',
    params: { id }
  })
}

/**
 * 根据 id 获取公告封装
 */
export function getAnnouncementVOById(id: string) {
  return request<AnnouncementVO>({
    url: '/api/announcement/get/vo',
    method: 'GET',
    params: { id }
  })
}

/**
 * 分页获取公告列表（仅管理员）
 */
export function listAnnouncementByPage(announcementQueryRequest: AnnouncementQueryRequest) {
  return request<{ records: any[]; total: number; current: number; size: number }>({
    url: '/api/announcement/list/page',
    method: 'POST',
    data: announcementQueryRequest
  })
}

/**
 * 分页获取公告封装列表
 */
export function listAnnouncementVOByPage(announcementQueryRequest: AnnouncementQueryRequest) {
  return request<{ records: AnnouncementVO[]; total: number; current: number; size: number }>({
    url: '/api/announcement/list/page/vo',
    method: 'POST',
    data: announcementQueryRequest
  })
}

/**
 * 发布公告（仅管理员）
 */
export function publishAnnouncement(id: string) {
  return request<boolean>({
    url: '/api/announcement/publish',
    method: 'POST',
    params: { id }
  })
}

/**
 * 下线公告（仅管理员）
 */
export function offlineAnnouncement(id: string) {
  return request<boolean>({
    url: '/api/announcement/offline',
    method: 'POST',
    params: { id }
  })
}

/**
 * 获取未读公告数量
 */
export function getUnreadCount() {
  return request<number>({
    url: '/api/announcement/unread/count',
    method: 'GET'
  })
}

/**
 * 标记公告为已读
 */
export function markAsRead(id: string) {
  return request<boolean>({
    url: '/api/announcement/read',
    method: 'POST',
    params: { id }
  })
}

/**
 * 获取公告列表（公开接口，只返回已发布的公告）
 */
export function listPublicAnnouncements(announcementQueryRequest: AnnouncementQueryRequest) {
  return request<{ records: AnnouncementVO[]; total: number; current: number; size: number }>({
    url: '/api/announcement/list/public',
    method: 'POST',
    data: announcementQueryRequest
  })
}
